<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //  race   all    allSettled
        let p1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(111)
            }, 1000);
        })

        let p2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                reject("error");
            }, 3000);
        })

        let p3 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(333)
            }, 2000);
        })

        // Promise.allSettled([p1, p2, p3]).then(res => console.log(res))
        /* 
        0: {status: 'fulfilled', value: 111}
        1: {status: 'rejected', reason: 'error'}
        2: {status: 'fulfilled', value: 333}
        */


        // 封装  手撸  allSettled
        function MyallSettled(arr) {
            return new Promise(resolve => {
                let resArr = new Array(arr.length);
                let count = 0;
                arr.forEach((p, key) => {
                    p.then(res => {
                        let obj = {};
                        obj.status = "fulfilled";
                        obj.value = res;
                        resArr[key] = obj;
                        count++;
                        if (count === arr.length) {
                            reslove(resArr);
                        }
                    }, err => {
                        let obj = {
                            status: "rejected",
                            reson: err
                        };
                        resArr[key] = obj;
                        count++;
                        if (count === arr.length) {
                            resolve(resArr);
                        }
                    })
                })
            })

        }
        MyallSettled([p1, p2, p3]).then(res => {
            console.log(res);
            /*     
            0: {status: 'fulfilled', value: 111}
            1: {status: 'rejected', reson: 'error'}
            2: {status: 'fulfilled', value: 333}
            */
        })
    </script>
</body>

</html>